/*
 * Licensed to the Apache Software Foundation (ASF) under one or more
 * contributor license agreements.  See the NOTICE file distributed with
 * this work for additional information regarding copyright ownership.
 * The ASF licenses this file to You under the Apache License, Version 2.0
 * (the "License"); you may not use this file except in compliance with
 * the License.  You may obtain a copy of the License at
 * 
 *      http://www.apache.org/licenses/LICENSE-2.0
 * 
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package org.cadoc.parser;

import org.cadoc.parser.statement.AssignmentStatement;
import org.cadoc.parser.statement.ClassDeclaration;
import org.cadoc.parser.statement.ConditionCheckStatement;
import org.cadoc.parser.statement.ImportStatement;
import org.cadoc.parser.statement.Interpretable;
import org.cadoc.parser.statement.IterationStatement;
import org.cadoc.parser.statement.Modifier;
import org.cadoc.parser.statement.OperationCallStatement;
import org.cadoc.parser.statement.OperationDeclaration;
import org.cadoc.parser.statement.PackageDeclaration;
import org.cadoc.parser.statement.Statement;
import org.cadoc.parser.statement.VariableDeclaration;

public class JavaStatementClassifier implements StatementClassifier {
	
	private StatementHandler statementHandler;
	
	public JavaStatementClassifier(StatementHandler statementHandler){
		this.statementHandler = statementHandler;
	}
	
	public void classify(String[] statement) {
		
		Statement stmt = null;
		String nextToken = null;
		String currentToken = null;
		boolean hasModifiers = isModifier(statement[0]);
		
		for(int i=0; i<statement.length; i++){
			
			if(i<(statement.length-1))
				nextToken = statement[i+1];
			else
				nextToken = null;
			
			currentToken = statement[i];
			if(currentToken.equals(LanguageToken.PACKAGE_DECLARATION)){
				stmt = new PackageDeclaration();
				stmt.setValue(nextToken);
				break;
			};
			if (currentToken.equals(LanguageToken.IMPORT_STATEMENT)){
				stmt = new ImportStatement();
				stmt.setValue(nextToken);
				break;
			};
			if (currentToken.equals(LanguageToken.CLASS_DECLARATION)){
				stmt = new ClassDeclaration();
				stmt.setValue(nextToken);
				break;
			};
			if (currentToken.equals(LanguageToken.CONDITIONAL_STATEMENT) || currentToken.contains(LanguageToken.CONDITIONAL_STATEMENT+"(") || currentToken.equals(LanguageToken.CONDITIONAL_STATEMENT2) || currentToken.contains("switch(")){
				stmt = new ConditionCheckStatement();
				break;
			};
			if (currentToken.equals(LanguageToken.ITERATION_STATEMENT)){
				stmt = new IterationStatement();
				break;
			};
			if (currentToken.equals(String.valueOf(LanguageToken.ASSIGNMENT_OPERATOR)) && i>1){
				stmt = new VariableDeclaration();
				((Interpretable)stmt).interpret(subArray(statement, i));
				statementHandler.startStatement(stmt);
			};
			if (currentToken.equals(String.valueOf(LanguageToken.ASSIGNMENT_OPERATOR)) || (currentToken.contains(LanguageToken.ASSIGNMENT_OPERATOR2)&&nextToken.charAt(0)==LanguageToken.PARAMETER_BEGIN)){
				stmt = new AssignmentStatement();
				break;
			};
			if (currentToken.contains(String.valueOf(LanguageToken.PARAMETER_BEGIN)) && hasModifiers){
				stmt = new OperationDeclaration();
				
				break;
			};
			if (currentToken.contains(String.valueOf(LanguageToken.PARAMETER_BEGIN)) && !hasModifiers){
				stmt = new OperationCallStatement();
				break;
			}
			
		}
		if (stmt == null && statement.length>=2){
			stmt = new VariableDeclaration();
		}
		
		if(stmt instanceof Interpretable){
			((Interpretable)stmt).interpret(statement);
		}
		
		statementHandler.startStatement(stmt);
		
	}

	private boolean isModifier(String stmtPart){
		for(Modifier modifier : LanguageToken.SUPPORTED_MODIFIERS){
			if(modifier.getName().equals(stmtPart)){
				return true;
			}
		}
		return false;
	}
	
	private String[] subArray(String[] array, int end){
		String[] subStmt = new String[end];
		for(int x=0; x<end;x++)subStmt[x]=array[x];
		return subStmt;
	}

}
